package com.neasho.neashoadminv1.infrastructure.domain.generator

import cn.hutool.core.util.StrUtil
import org.babyfish.jimmer.Formula
import org.babyfish.jimmer.sql.Entity
import org.babyfish.jimmer.sql.Id
import org.babyfish.jimmer.sql.Table

@Entity
@Table(name = "information_schema.columns")
interface AppTableColumn {
    val tableSchema: String
    val tableName: String
    @Id
    val columnName: String
    val columnComment: String?
    val columnType: String
    val columnKey: String
    val columnDefault: String?
    val extra: String?

    @Formula(dependencies = ["columnName"])
    val fieldName:String
        get() = StrUtil.toCamelCase(columnName)

    @Formula(dependencies = ["columnName", "tableName"])
    val enumName:String
        get() = StrUtil.upperFirst(StrUtil.toCamelCase(tableName+"_"+columnName))

    @Formula(dependencies = ["columnType"])
    val isEnum: Boolean
        get() = columnType.startsWith("enum")

    @Formula(dependencies = ["columnType"])
    val jdbcType: String
        get() = when {
            columnType.startsWith("bigint") -> "Long"
            columnType.contains("int") -> "Int"
            columnType.contains("char") -> "String"
            columnType.contains("text") -> "String"
            columnType.contains("date") -> "LocalDate"
            columnType.contains("time") -> "TIME"
            columnType.contains("datetime") -> "LocalDateTime"
            columnType.contains("decimal") -> "BigDecimal"
            columnType.contains("float") -> "Float"
            columnType.contains("double") -> "Double"
            columnType.startsWith("enum") -> "Enum"
            else -> "String"
        }

    @Formula(dependencies = ["columnType"])
    val enumItems: List<String>
        get() {
            return columnType
                .substring(columnType.indexOf("(")+1, columnType.indexOf(")" ))
                .split(",")
                .map { it.trim().replace("'", "") }
        }
}
